package com.foreknow.controller;

import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
import com.foreknow.util.Pagination;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet{
    private int pageSize=10;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //我们只需要在doget方法中获取请求参数，然后根据获取到的参数来决定调用哪个方法
        String method=req.getParameter("q");
        if ("delete".equals(method)){
            delete(req,resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("idList".equals(method)) {
            idList(req, resp);
        }else{
            list(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        //要获取到web.xml文件中配置的全局初始化参数
        this.pageSize=Integer.parseInt(config.getServletContext().getInitParameter("pageSize"));

    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有留言信息
        //要从页面中获取的页码
        int pageNumber=0;
        try {
            pageNumber = Integer.parseInt(req.getParameter("pageNo"));//获取到web中的全局变量（每页显示数据条数）
        }catch (Exception e){
            pageNumber=1;
        }
        GuestbookService service =new GuestbookServiceImpl();
        Pagination pagination = service.getByPage(pageSize,pageNumber);//传递分页大小和要显示的页码
        //获取区间数据
        MappingFactory mappingFactory=MappingFactory.getInstance();
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = pagination.getList(mapping);
        //将查询到的list保存到服务器
        req.setAttribute("list",list);
        //将总记录数保存到req中
        req.setAttribute("total",pagination.getMaxElements());
        //请求转发给jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除留言
        GuestbookService service =new GuestbookServiceImpl();
        String id=req.getParameter("id");
        //调用service层的删除方法
        boolean isRight=service.deleteBean(Integer.parseInt(id));
        if (isRight){
            //删除后调用查询所有方法刷新页面信息
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改留言信息
        //获取网页传来的数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String createdtime = req.getParameter("createdtime");
        Guestbook gb=new Guestbook();
        gb.setId(Integer.parseInt(id));
        gb.setName(name);
        gb.setPhone(phone);
        gb.setEmail(email);
        gb.setTitle(title);
        gb.setContent(content);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyy-MM-dd");
        gb.setCreatedtime(simpleDateFormat.format(new Date()));
        //调用service层的id修改方法
        GuestbookService service=new GuestbookServiceImpl();
        boolean isRight = service.updateBean(gb);
        if (isRight){
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }else{
            PrintWriter out = resp.getWriter();
            out.println("未修改成功");
        }
    }

    protected void idList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id查询留言信息
        //获取服务器端的参数r
        String id = req.getParameter("id");
        //调用service层的查询方法
        GuestbookService service=new GuestbookServiceImpl();
        //调用service层的查询方法并返回一个对象
        Guestbook guestbook = service.idQueryInfo(Integer.parseInt(id));
        //将查询到的对象保存到服务器端(以键值对的形式）
        req.setAttribute("gb",guestbook);
        //请求转发 到jsp页面
        req.getRequestDispatcher("/idList.jsp").forward(req,resp);
    }



}
